package com.kobe.system.controller;

import com.kobe.common.core.controller.BaseController;
import com.kobe.common.core.page.TableDataInfo;
import com.kobe.system.domain.SysKobeFinance;
import com.kobe.system.domain.SysKobeProject;
import com.kobe.system.service.SysKobeFinanceService;
import com.kobe.system.service.SysKobeProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/kobe/money")
public class SysKobeProjectController extends BaseController {

    @Autowired
    private SysKobeProjectService sysKobeProjectService;

    @Autowired
    private SysKobeFinanceService sysKobeFinanceService;



    @GetMapping("/listProject")
    public TableDataInfo listProject(SysKobeProject sysKobeProject) {
        startPage();
        List<SysKobeProject> list = sysKobeProjectService.listProject(sysKobeProject);
        return getDataTable( list);
    }

    @GetMapping("/getProjectMoney/{projectId}")
    public Map<String, Double> getProject(@PathVariable Long projectId) {
        Map<String, Double> money = new HashMap<>();

        Double funds = sysKobeProjectService.getProjectTotalMoneyById(projectId);
        Double come = sysKobeFinanceService.getProjectComeMoneyById(projectId);
        Double out = sysKobeFinanceService.getProjectOutMoneyById(projectId);
        // 可以添加日志来查看具体值
        logger.info("Project ID: {}, Come: {}, Out: {}", projectId, come, out);
        // 更详细的空值处理
        Double fundsValue = funds != null ? funds : 0.0;
        Double comeValue = come != null ? come : 0.0;
        Double outValue = out != null ? out : 0.0;
        double calculation = comeValue - outValue;
        money.put("Calculation", calculation);
        money.put("funds", fundsValue);
        money.put("come", comeValue);
        money.put("out", outValue);

        double unliquidated = Math.max(fundsValue - comeValue, 0) ;
        money.put("unliquidated", unliquidated);
        return money;
    }


}
